<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>修改管理</title>
    <link href="css/style.css" rel="stylesheet" type="text/css"/>
    <script language="javascript" type="text/javascript" src="js/jquery.js"></script>
    <script language="javascript" type="text/javascript" src="js/My97DatePicker/WdatePicker.js"></script>
</head>

<body>

<div class="place">
    <span>位置：</span>
    <ul class="placeul">
        <li><a href="#">修改图书</a></li>
    </ul>
</div>

<div class="formbody">

    <div class="formtitle"><span>图书信息</span></div>

    <form action="bookServlet" method="post">
        <input name="mark" value="update" type="hidden"/>
        <input name="bid" value="" type="hidden"/>
        <ul class="forminfo">


            <li>
                <label>图书分类</label>
                <select name="cid" class="dfinput">
                    <option value="0">===请选择分类===</option>
                </select>
            </li>

            <li><label>图书名称</label><input name="bname" type="text" class="dfinput"/><i>商品名称不能超过20个字符</i></li>
            <li><label>作者</label><input name="author" type="text" class="dfinput"/></li>
            <li><label>图书报价</label><input name="price" type="text" class="dfinput"/></li>
            <li><label>简介</label>
                <textarea name="description" cols="10" rows="10" class="textinput" style="height: 80px"></textarea>
            </li>
            <li><label>版本</label><input name="version" type="text" class="dfinput"/></li>
            <li><label>热推</label>
                <cite>
                    <input name="state" type="radio" value="0" checked="checked"/>正常&nbsp;&nbsp;&nbsp;&nbsp;
                    <input name="state" type="radio" value="1"/>火热
                    <input name="state" type="radio" value="2"/>推荐
                    <input name="state" type="radio" value="3"/>天地推理
                </cite>
            </li>
            <li><label>出版日期</label>
                <input class="Wdate" style="width: 345px;height: 32px;line-height: 32px;"
                       onclick="WdatePicker({el:this,dateFmt:'yyyy-MM-dd HH:mm:ss'})" name="book_date" type="text"
                       class="dfinput" value=""/>
            </li>

            <li><label>&nbsp;</label><input type="submit" class="btn" value="确认保存"/></li>
        </ul>
    </form>
</div>
<script>
    // 获取书本分类
    $.ajax({
        url: "category",
        type: "post",
        data: {mark: "selectAll"},
        dataType: "text",
        success: function (data) {
            var cate = JSON.parse(data);
            var opt = ""
            $(cate).each(function () {
                opt += '<option value="' + this.cid + '">' + this.cname + '</option>'
            })
            $("[name='cid']").append(opt)
        },
        error: function () {
            alert("请求失败");
        }
    })

    var bid = GetQueryString("bid")

    $.ajax({
        url: "bookServlet",
        type: "post",
        data: {mark: "getBookInfo", bid: bid},
        dataType: "text",
        success: function (data) {
            var book = JSON.parse(data)
            console.log(book)
            $("[name='bid']").val(bid)
            $("[name='cid']").val(book.cid)
            $("[name='bname']").val(book.bname)
            $("[name='author']").val(book.author)
            $("[name='price']").val(book.price)
            $("[name='description']").val(book.description)
            $("input[type='radio'][name='state'][value='"+book.state+"']").prop("checked", "true")
            $("[name='version']").val(book.version)
            $("[name='book_date']").val(book.book_date)
        },
        error: function () {
            alert("请求失败")
        }
    })


    function GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg); //获取url中"?"符后的字符串并正则匹配
        var context = "";
        if (r != null)
            context = decodeURIComponent(r[2]);
        reg = null;
        r = null;
        return context == null || context == "" || context == "undefined" ? "" : context;
    }
</script>
</body>
</html>
